﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FlatRedBall;
using FlatRedBall.Input;

namespace BulletHellBoxes.Utilities
{
    public enum ButtonState
    {
        Down,
        Pushed,
        Released,
        Up
    };

    public class GameController
    {
        #region Fields
        private int _controllerIndex;
        private bool _rightSide;

        private ButtonState _currentTriggerState;
        private ButtonState _previousTriggerState;
        #endregion

        #region Properties
        public AnalogButton Trigger
        {
            get
            {
                if (_rightSide)
                    return InputManager.Xbox360GamePads[_controllerIndex].RightTrigger;
                else
                    return InputManager.Xbox360GamePads[_controllerIndex].LeftTrigger;
            }
        }
        public AnalogStick Stick
        {
            get
            {
                if (_rightSide)
                    return InputManager.Xbox360GamePads[_controllerIndex].RightStick;
                else
                    return InputManager.Xbox360GamePads[_controllerIndex].LeftStick;
            }
        }
        public ButtonState StartButton
        {
            get
            {
                Xbox360GamePad gamepad = InputManager.Xbox360GamePads[_controllerIndex];
                Xbox360GamePad.Button gameButton;

                if (_rightSide)
                {
                    gameButton = Xbox360GamePad.Button.Start;
                }
                else
                {
                    gameButton = Xbox360GamePad.Button.Back;
                }

                if (gamepad.ButtonPushed(gameButton))
                    return ButtonState.Pushed;
                else if (gamepad.ButtonReleased(gameButton))
                    return ButtonState.Released;
                else if (gamepad.ButtonDown(gameButton))
                    return ButtonState.Down;
                else
                    return ButtonState.Up;

            }
        }
        public ButtonState TriggerButton
        {
            get
            {
                return _currentTriggerState;
            }
        }
        #endregion

        #region Constructors
        public GameController(int controllerIndex, bool isRight)
        {
            _controllerIndex = controllerIndex;
            _rightSide = isRight;

            _currentTriggerState = ButtonState.Up;
            _previousTriggerState = ButtonState.Up;
        }
        #endregion

        #region Public Methods
        public void Update()
        {
            ButtonState oldState = _currentTriggerState;

            switch (_previousTriggerState)
            {
                case ButtonState.Up:
                    if (Trigger.Position > 0.2f)
                        _currentTriggerState = ButtonState.Pushed;
                    else
                        _currentTriggerState = ButtonState.Up;
                    break;
                case ButtonState.Pushed:
                    _currentTriggerState = ButtonState.Down;
                    break;
                case ButtonState.Released:
                    _currentTriggerState = ButtonState.Up;
                    break;
                case ButtonState.Down:
                    if (Trigger.Position > 0.2f)
                        _currentTriggerState = ButtonState.Down;
                    else
                        _currentTriggerState = ButtonState.Released;
                    break;
            }

            _previousTriggerState = oldState;
        }

        #endregion
    }
}
